<!DOCTYPE html>
<html lang="zh-cn">
<head prefix="og: http://ogp.me/ns#">
  <meta charset="utf-8">
  <title>标签插件（Tag） | Hexo</title>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Canonical links -->
  <link rel="canonical" href="https://hexo.io/zh-cn/api/tag.html">
  <!-- Alternative links -->
  
    
      <link rel="alternative" hreflang="en" href="https://hexo.io/api/tag.html">
    
      <link rel="alternative" hreflang="zh-tw" href="https://hexo.io/zh-tw/api/tag.html">
    
      <link rel="alternative" hreflang="zh-cn" href="https://hexo.io/zh-cn/api/tag.html">
    
      <link rel="alternative" hreflang="ru" href="https://hexo.io/ru/api/tag.html">
    
      <link rel="alternative" hreflang="ko" href="https://hexo.io/ko/api/tag.html">
    
  
  <!-- Icon -->
  <link rel="apple-touch-icon" sizes="57x57" href="/icon/apple-touch-icon-57x57.png">
  <link rel="apple-touch-icon" sizes="114x114" href="/icon/apple-touch-icon-114x114.png">
  <link rel="apple-touch-icon" sizes="72x72" href="/icon/apple-touch-icon-72x72.png">
  <link rel="apple-touch-icon" sizes="144x144" href="/icon/apple-touch-icon-144x144.png">
  <link rel="apple-touch-icon" sizes="60x60" href="/icon/apple-touch-icon-60x60.png">
  <link rel="apple-touch-icon" sizes="120x120" href="/icon/apple-touch-icon-120x120.png">
  <link rel="apple-touch-icon" sizes="76x76" href="/icon/apple-touch-icon-76x76.png">
  <link rel="apple-touch-icon" sizes="152x152" href="/icon/apple-touch-icon-152x152.png">
  <link rel="icon" type="image/png" href="/icon/favicon-196x196.png" sizes="196x196">
  <link rel="icon" type="image/png" href="/icon/favicon-160x160.png" sizes="160x160">
  <link rel="icon" type="image/png" href="/icon/favicon-96x96.png" sizes="96x96">
  <link rel="icon" type="image/png" href="/icon/favicon-16x16.png" sizes="16x16">
  <link rel="icon" type="image/png" href="/icon/favicon-32x32.png" sizes="32x32">
  <meta name="msapplication-TileColor" content="#2f83cd">
  <meta name="msapplication-TileImage" content="/icon/mstile-144x144.png">
  <!-- CSS -->
  <!-- build:css build/css/navy.css -->
  <link rel="stylesheet" href="/css/navy.css">
  <!-- endbuild -->
  <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.css">
  <!-- RSS -->
  <link rel="alternate" href="/atom.xml" title="Hexo">
  <!-- Open Graph -->
  <meta name="description" content="标签插件帮助开发者在文章中快速插入内容。 概要hexo.extend.tag.register(name, function(args, content)&amp;#123;&amp;#125;, options); 标签函数会传入两个参数：args 和 content，前者代表开发者在使用标签插件时传入的参数，而后者则是标签插件所覆盖的内容。 从 Hexo 3 开始，因为新增了非同步渲染功能，而改用 Nunju">
<meta property="og:type" content="website">
<meta property="og:title" content="标签插件（Tag）">
<meta property="og:url" content="https://hexo.io/zh-cn/api/tag.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="标签插件帮助开发者在文章中快速插入内容。 概要hexo.extend.tag.register(name, function(args, content)&amp;#123;&amp;#125;, options); 标签函数会传入两个参数：args 和 content，前者代表开发者在使用标签插件时传入的参数，而后者则是标签插件所覆盖的内容。 从 Hexo 3 开始，因为新增了非同步渲染功能，而改用 Nunju">
<meta property="og:locale" content="zh-cn">
<meta property="og:updated_time" content="2017-12-06T21:52:16.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="标签插件（Tag）">
<meta name="twitter:description" content="标签插件帮助开发者在文章中快速插入内容。 概要hexo.extend.tag.register(name, function(args, content)&amp;#123;&amp;#125;, options); 标签函数会传入两个参数：args 和 content，前者代表开发者在使用标签插件时传入的参数，而后者则是标签插件所覆盖的内容。 从 Hexo 3 开始，因为新增了非同步渲染功能，而改用 Nunju">
<meta name="twitter:site" content="hexojs">
<meta property="fb:admins" content="100000247608790">
  <!-- Google Analytics -->
  
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-48498357-3', 'auto');
  ga('send', 'pageview');
</script>

</head>

<body>
  <div id="container">
    <header id="header" class="wrapper">
  <div id="header-inner" class="inner">
    <h1 id="logo-wrap">
      <a href="/zh-cn/" id="logo">Hexo</a>
    </h1>
    <nav id="main-nav">
      <a href="/zh-cn/docs/" class="main-nav-link">文档</a><a href="/zh-cn/api/" class="main-nav-link">API</a><a href="/news/" class="main-nav-link">新闻</a><a href="/plugins/" class="main-nav-link">插件</a><a href="/themes/" class="main-nav-link">主题</a>
      <a href="https://github.com/hexojs/hexo" class="main-nav-link"><i class="fa fa-github-alt"></i></a>
      <div id="search-input-wrap">
        <div id="search-input-icon">
          <i class="fa fa-search"></i>
        </div>
        <input type="search" id="search-input" placeholder="Search...">
      </div>
    </nav>
    <div id="lang-select-wrap">
      <label id="lang-select-label"><i class="fa fa-globe"></i><span>简体中文</span></label>
      <select id="lang-select" data-canonical="api/tag.html">
        
          <option value="en">English</option>
        
          <option value="zh-tw">正體中文</option>
        
          <option value="zh-cn" selected>简体中文</option>
        
          <option value="ru">Русский</option>
        
          <option value="ko">한국어</option>
        
      </select>
    </div>
    <a id="mobile-nav-toggle">
      <span class="mobile-nav-toggle-bar"></span>
      <span class="mobile-nav-toggle-bar"></span>
      <span class="mobile-nav-toggle-bar"></span>
    </a>
  </div>
</header>

    <div id="content-wrap">
  <div id="content" class="wrapper">
    <div id="content-inner">
      <article class="article-container" itemscope itemtype="http://schema.org/Article">
        <div class="article-inner">
          <div class="article">
            <div class="inner">
              <header class="article-header">
                <h1 class="article-title" itemprop="name">标签插件（Tag）</h1>
                <a href="https://github.com/hexojs/site/edit/master/source/zh-cn/api/tag.md" class="article-edit-link" title="改进本文"><i class="fa fa-pencil"></i></a>
              </header>
              <div class="article-content" itemprop="articleBody">
                <p>标签插件帮助开发者在文章中快速插入内容。</p>
<h2 id="概要" class="article-heading"><a href="#概要" class="headerlink" title="概要"></a>概要<a class="article-anchor" href="#概要" aria-hidden="true"></a></h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">hexo.extend.tag.register(name, <span class="function"><span class="keyword">function</span>(<span class="params">args, content</span>)</span>&#123;</span><br><span class="line">&#125;, options);</span><br></pre></td></tr></table></figure>
<p>标签函数会传入两个参数：<code>args</code> 和 <code>content</code>，前者代表开发者在使用标签插件时传入的参数，而后者则是标签插件所覆盖的内容。</p>
<p>从 Hexo 3 开始，因为新增了非同步渲染功能，而改用 <a href="http://mozilla.github.io/nunjucks/" target="_blank" rel="noopener">Nunjucks</a> 作为渲染引擎，其行为可能会与过去使用的 <a href="http://paularmstrong.github.io/swig/" target="_blank" rel="noopener">Swig</a> 有些许差异。</p>
<h2 id="选项" class="article-heading"><a href="#选项" class="headerlink" title="选项"></a>选项<a class="article-anchor" href="#选项" aria-hidden="true"></a></h2><h3 id="ends" class="article-heading"><a href="#ends" class="headerlink" title="ends"></a>ends<a class="article-anchor" href="#ends" aria-hidden="true"></a></h3><p>使用结束标签，此选项默认为 <code>false</code>。</p>
<h3 id="async" class="article-heading"><a href="#async" class="headerlink" title="async"></a>async<a class="article-anchor" href="#async" aria-hidden="true"></a></h3><p>开启非同步模式，此选项默认为 <code>false</code>。</p>
<h2 id="范例" class="article-heading"><a href="#范例" class="headerlink" title="范例"></a>范例<a class="article-anchor" href="#范例" aria-hidden="true"></a></h2><h3 id="没有结束标签" class="article-heading"><a href="#没有结束标签" class="headerlink" title="没有结束标签"></a>没有结束标签<a class="article-anchor" href="#没有结束标签" aria-hidden="true"></a></h3><p>插入 Youtube 影片。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">hexo.extend.tag.register(<span class="string">'youtube'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">args</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> id = args[<span class="number">0</span>];</span><br><span class="line">  <span class="keyword">return</span> <span class="string">'&lt;div class="video-container"&gt;&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/'</span> + id + <span class="string">'" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;&lt;/div&gt;'</span>;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="有结束标签" class="article-heading"><a href="#有结束标签" class="headerlink" title="有结束标签"></a>有结束标签<a class="article-anchor" href="#有结束标签" aria-hidden="true"></a></h3><p>插入 pull quote。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">hexo.extend.tag.register(<span class="string">'pullquote'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">args, content</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> className =  args.join(<span class="string">' '</span>);</span><br><span class="line">  <span class="keyword">return</span> <span class="string">'&lt;blockquote class="pullquote'</span> + className + <span class="string">'"&gt;'</span> + content + <span class="string">'&lt;/blockquote&gt;'</span>;</span><br><span class="line">&#125;, &#123;<span class="attr">ends</span>: <span class="literal">true</span>&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="非同步渲染" class="article-heading"><a href="#非同步渲染" class="headerlink" title="非同步渲染"></a>非同步渲染<a class="article-anchor" href="#非同步渲染" aria-hidden="true"></a></h3><p>插入文件。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> fs = <span class="built_in">require</span>(<span class="string">'hexo-fs'</span>);</span><br><span class="line"><span class="keyword">var</span> pathFn = <span class="built_in">require</span>(<span class="string">'path'</span>);</span><br><span class="line"></span><br><span class="line">hexo.extend.tag.register(<span class="string">'include_code'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">args</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> filename = args[<span class="number">0</span>];</span><br><span class="line">  <span class="keyword">var</span> path = pathFn.join(hexo.source_dir, filename);</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">return</span> fs.readFile(path).then(<span class="function"><span class="keyword">function</span>(<span class="params">content</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">'&lt;pre&gt;&lt;code&gt;'</span> + content + <span class="string">'&lt;/code&gt;&lt;/pre&gt;'</span>;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;, &#123;<span class="attr">async</span>: <span class="literal">true</span>&#125;);</span><br></pre></td></tr></table></figure>

              </div>
              <footer class="article-footer">
                <time class="article-footer-updated" datetime="2017-12-06T21:52:16.000Z" itemprop="dateModified">上次更新：2017-12-06</time>
                <a href="renderer.html" class="article-footer-prev" title="渲染引擎 (Renderer)"><i class="fa fa-chevron-left"></i><span>上一页</span></a>
              </footer>
              
<section id="comments">
  <div id="disqus_thread"></div>
</section>
<script>
  var disqus_shortname = 'hexojs';
  var disqus_url = 'https://hexo.io/zh-cn/api/tag.html';
  var disqus_title = "标签插件（Tag）";
  var disqus_config = function(){
    this.language = 'zh';
  };
  (function(){
    var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
    dsq.src = 'https://go.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>

            </div>
          </div>
          <aside id="article-toc" role="navigation">
            <div id="article-toc-inner">
              <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=C6AILKT&placement=hexoio" id="_carbonads_js"></script>
              <strong class="sidebar-title">目录</strong>
              <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#概要"><span class="toc-text"><a href="#&#x6982;&#x8981;" class="headerlink" title="&#x6982;&#x8981;"></a>&#x6982;&#x8981;</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#选项"><span class="toc-text"><a href="#&#x9009;&#x9879;" class="headerlink" title="&#x9009;&#x9879;"></a>&#x9009;&#x9879;</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#ends"><span class="toc-text"><a href="#ends" class="headerlink" title="ends"></a>ends</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#async"><span class="toc-text"><a href="#async" class="headerlink" title="async"></a>async</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#范例"><span class="toc-text"><a href="#&#x8303;&#x4F8B;" class="headerlink" title="&#x8303;&#x4F8B;"></a>&#x8303;&#x4F8B;</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#没有结束标签"><span class="toc-text"><a href="#&#x6CA1;&#x6709;&#x7ED3;&#x675F;&#x6807;&#x7B7E;" class="headerlink" title="&#x6CA1;&#x6709;&#x7ED3;&#x675F;&#x6807;&#x7B7E;"></a>&#x6CA1;&#x6709;&#x7ED3;&#x675F;&#x6807;&#x7B7E;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#有结束标签"><span class="toc-text"><a href="#&#x6709;&#x7ED3;&#x675F;&#x6807;&#x7B7E;" class="headerlink" title="&#x6709;&#x7ED3;&#x675F;&#x6807;&#x7B7E;"></a>&#x6709;&#x7ED3;&#x675F;&#x6807;&#x7B7E;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#非同步渲染"><span class="toc-text"><a href="#&#x975E;&#x540C;&#x6B65;&#x6E32;&#x67D3;" class="headerlink" title="&#x975E;&#x540C;&#x6B65;&#x6E32;&#x67D3;"></a>&#x975E;&#x540C;&#x6B65;&#x6E32;&#x67D3;</span></a></li></ol></li></ol>
              <a href="#" id="article-toc-top">回到顶部</a>
            </div>
          </aside>
        </div>
      </article>
      <aside id="sidebar" role="navigation">
  <div class="inner">
    <strong class="sidebar-title">核心</strong><a href="index.html" class="sidebar-link">概述</a><a href="events.html" class="sidebar-link">事件</a><a href="locals.html" class="sidebar-link">本地变量</a><a href="router.html" class="sidebar-link">路由</a><a href="box.html" class="sidebar-link">盒(Box)</a><a href="rendering.html" class="sidebar-link">渲染</a><a href="posts.html" class="sidebar-link">文章</a><a href="scaffolds.html" class="sidebar-link">模版 (Scaffold)</a><a href="themes.html" class="sidebar-link">主题</a><strong class="sidebar-title">扩展</strong><a href="console.html" class="sidebar-link">控制台 (Console)</a><a href="deployer.html" class="sidebar-link">部署器 (Deployer)</a><a href="filter.html" class="sidebar-link">过滤器 (Filter)</a><a href="generator.html" class="sidebar-link">生成器 (Generator)</a><a href="helper.html" class="sidebar-link">辅助函数 (Helper)</a><a href="migrator.html" class="sidebar-link">迁移器 (Migrator)</a><a href="processor.html" class="sidebar-link">处理器 (Processor)</a><a href="renderer.html" class="sidebar-link">渲染引擎 (Renderer)</a><a href="tag.html" class="sidebar-link current">标签 (Tag)</a>
  </div>
</aside>
    </div>
  </div>
</div>

    <footer id="footer" class="wrapper">
  <div class="inner">
    <div id="footer-copyright">
      &copy; 2017 <a href="https://github.com/hexojs/hexo/graphs/contributors" target="_blank">Hexo</a><br>
      Documentation licensed under <a href="http://creativecommons.org/licenses/by/4.0/" target="_blank">CC BY 4.0</a>.
    </div>
    <div id="footer-links">
      <a href="https://twitter.com/hexojs" class="footer-link" target="_blank"><i class="fa fa-twitter"></i></a>
      <a href="https://github.com/hexojs/hexo" class="footer-link" target="_blank"><i class="fa fa-github-alt"></i></a>
    </div>
  </div>
</footer>

  </div>
  <div id="mobile-nav-dimmer"></div>
  <nav id="mobile-nav">
  <div id="mobile-nav-inner">
    <ul id="mobile-nav-list">
      <a href="/zh-cn/docs/" class="mobile-nav-link">文档</a><a href="/zh-cn/api/" class="mobile-nav-link">API</a><a href="/news/" class="mobile-nav-link">新闻</a><a href="/plugins/" class="mobile-nav-link">插件</a><a href="/themes/" class="mobile-nav-link">主题</a>
      <li class="mobile-nav-item">
        <a href="https://github.com/hexojs/hexo" class="mobile-nav-link" rel="external" target="_blank">GitHub</a>
      </li>
    </ul>
    
      <strong class="mobile-nav-title">核心</strong><a href="index.html" class="mobile-nav-link">概述</a><a href="events.html" class="mobile-nav-link">事件</a><a href="locals.html" class="mobile-nav-link">本地变量</a><a href="router.html" class="mobile-nav-link">路由</a><a href="box.html" class="mobile-nav-link">盒(Box)</a><a href="rendering.html" class="mobile-nav-link">渲染</a><a href="posts.html" class="mobile-nav-link">文章</a><a href="scaffolds.html" class="mobile-nav-link">模版 (Scaffold)</a><a href="themes.html" class="mobile-nav-link">主题</a><strong class="mobile-nav-title">扩展</strong><a href="console.html" class="mobile-nav-link">控制台 (Console)</a><a href="deployer.html" class="mobile-nav-link">部署器 (Deployer)</a><a href="filter.html" class="mobile-nav-link">过滤器 (Filter)</a><a href="generator.html" class="mobile-nav-link">生成器 (Generator)</a><a href="helper.html" class="mobile-nav-link">辅助函数 (Helper)</a><a href="migrator.html" class="mobile-nav-link">迁移器 (Migrator)</a><a href="processor.html" class="mobile-nav-link">处理器 (Processor)</a><a href="renderer.html" class="mobile-nav-link">渲染引擎 (Renderer)</a><a href="tag.html" class="mobile-nav-link current">标签 (Tag)</a>
    
  </div>
  <div id="mobile-lang-select-wrap">
    <span id="mobile-lang-select-label"><i class="fa fa-globe"></i><span>简体中文</span></span>
    <select id="mobile-lang-select" data-canonical="api/tag.html">
      
        <option value="en">English</option>
      
        <option value="zh-tw">正體中文</option>
      
        <option value="zh-cn" selected>简体中文</option>
      
        <option value="ru">Русский</option>
      
        <option value="ko">한국어</option>
      
    </select>
  </div>
</nav>
  <!-- Scripts -->
<!-- build:js build/js/main.js -->
<script src="/js/lang_select.js"></script>
<script src="/js/toc.js"></script>
<script src="/js/mobile_nav.js"></script>
<!-- endbuild -->

<!-- Algolia -->

<script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.js"></script>
<script type="text/javascript">
document.getElementById('search-input-wrap').classList.add('on');
docsearch({
  apiKey: 'cdea7d4e47d84f6145b77e7f68689f35',
  indexName: 'hexo_zh-cn',
  inputSelector: '#search-input'
});
</script>


</body>
</html>